package com.dtw.ditouwang.ui.more;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.MoreBean;
import com.dtw.ditouwang.models.WebApi;
import com.dtw.ditouwang.ui.OnItemClickListener;
import com.dtw.ditouwang.views.GlideRoundTransform;

import java.util.List;

public class EditorChooseNormalAdapter extends RecyclerView.Adapter<EditorChooseNormalAdapter.Holder> {
    private List<MoreBean.DataBean> dataBeanList;
    RequestOptions requestOptions = RequestOptions.bitmapTransform(new GlideRoundTransform(10));
    private OnItemClickListener onItemClickListener;

    public EditorChooseNormalAdapter(List<MoreBean.DataBean> dataBeanList) {

        this.dataBeanList = dataBeanList;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {

        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new Holder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_editor_choose_normal, viewGroup, false));
    }

    @Override
    public int getItemCount() {
        return dataBeanList.size();
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int i) {
        Glide
                .with(holder.imageView)
                .load(WebApi.BaseUrl + dataBeanList.get(i).getCover().replace("\\", "/"))
                .apply(requestOptions)
                .into(holder.imageView);
        holder.content.setText(dataBeanList.get(i).getTitle());
        holder.comment.setText(dataBeanList.get(i).getComments() + "");
        holder.like.setText(dataBeanList.get(i).getSee() + "");
    }

    class Holder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView content, comment, like;

        public Holder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image_image);
            content = itemView.findViewById(R.id.text_content);
            comment = itemView.findViewById(R.id.text_comment);
            like = itemView.findViewById(R.id.text_like);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(0, getAdapterPosition(), 0);
                    }
                }
            });
        }
    }
}
